package com.venus.finance.chart.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.venus.common.action.BaseAction;
import com.venus.common.utils.ConditionUtil;
import com.venus.common.utils.Constants;
import com.venus.common.utils.DateUtil;
import com.venus.common.utils.MathUtil;
import com.venus.finance.chart.form.ChartForm;
import com.venus.finance.fee.bean.FeeBean;
import com.venus.finance.fee.bean.MonthLineBean;
import com.venus.finance.fee.service.IFeeService;
import com.venus.finance.transferaccount.bean.TransferAccountBean;
import com.venus.finance.transferaccount.service.ITransferAccountService;
import com.venus.model.PubUserVO;

public class ChartMultiOperateAction extends BaseAction{

	private IFeeService feeService;

	private ITransferAccountService transferAccountService;
	
	public void setFeeService(IFeeService feeService) {this.feeService = feeService;}

	public void setTransferAccountService(ITransferAccountService transferAccountService) {this.transferAccountService = transferAccountService;}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		ChartForm objChartForm = (ChartForm)form;
		String strMethod = objChartForm.getMethod();
		if("getQuickChart".equals(strMethod)){
			return this.getQuickChart(mapping, objChartForm, request, response);
		}else if("getPieChart".equals(strMethod)){
			return this.getPieChart(mapping, objChartForm, request, response);
		}else if(("getInPkOutPieChart").equals(strMethod)){
			return this.getInPkOutPieChart(mapping, objChartForm, request, response);
		}else if("getLineChart".equals(strMethod)){
			return this.getLineChart(mapping, objChartForm, request, response);
		}else if("getSubPie".equals(strMethod)){
			return this.getSubPie(mapping, objChartForm, request, response);
		}else if(("getComboChart").equals(strMethod)){
			return this.getComboChart(mapping, objChartForm, request, response);
		}else if(("initGetContributeChart").equals(strMethod)){
			return this.initGetContributeChart(mapping, objChartForm, request, response);
		}else if(("getContributeChart").equals(strMethod)){
			return this.getContributeChart(mapping, objChartForm, request, response);
		}else if(("initGetOutInBalanceChart").equals(strMethod)){
			return this.initGetOutInBalanceChart(mapping, objChartForm, request, response);
		}else{
			this.log.error("没有找到访问的方法!方法名: "+strMethod);
			return null;
		}
	}
	
	public ActionForward getQuickChart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("quickchart");
	}
	
	public ActionForward getPieChart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		ChartForm objChartForm = (ChartForm)form;
		// 页面传递过来的当前登陆用户
		PubUserVO objPubUserVO = this.getLoginUser(request);
		// 准备查询当月数据
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		// 管理员可以查看组所有数据
		if(!objPubUserVO.equals(objPubUserVO.getUserGroup().getGroupAdmin())){
			lstConditionUtils.add(new ConditionUtil("and", "FeeVO.pubUser.userId", "=", objPubUserVO.getUserId()+"", false));
		}
		lstConditionUtils.add(new ConditionUtil("and", "FeeVO.feeType.feeTypeId", "=", "FeeTypeVO.feeTypeId", false));
		lstConditionUtils.add(new ConditionUtil("and", "FeeTypeVO.feeType", "=", objChartForm.getFeeType()+"", false));
		lstConditionUtils.add(new ConditionUtil("between", "FeeVO.feeDate", "", objChartForm.getStartDate()+","+objChartForm.getEndDate(), true));
		lstConditionUtils.add(new ConditionUtil("group", "FeeVO.feeType.feeTypeId", "", "", false));
		// 返回汇总 [消费类型ID,消费类型名称,消费金额]
		List<Object[]> lstDatas = this.feeService.groupFeeVOByFeeTypeId(lstConditionUtils);
		StringBuffer sb = new StringBuffer(128);
		sb.append("{");
		sb.append("\"title\":\""+objChartForm.getStartDate()+","+objChartForm.getEndDate()+"\",");
		sb.append("\"data\":[");
		float fcount = 0f;
		for(int i=0;i<lstDatas.size();i++) {
			Object[] objs = lstDatas.get(i);
			sb.append("{");
			sb.append("\"name\":");
			sb.append("\""+objs[1]+"\"");
			sb.append(",\"y\":");
			// 格式化金额
			sb.append(MathUtil.round((Double)objs[2], 2));
			sb.append(",\"feeTypeId\":");
			sb.append(objs[0]);
			sb.append(",\"chartType\":");
			sb.append("\""+objChartForm.getChartType()+"\"");
			sb.append("}");
			if(i<lstDatas.size()-1){
				sb.append(",");
			}
			// 总金额
			fcount = MathUtil.round(fcount+Float.parseFloat(objs[2].toString()), 2).floatValue();
		}
		sb.append("]");
		sb.append(",\"subtitle\":\""+(objChartForm.getFeeType()==0?"总支出":"总收入")+":"+fcount+"\"");
		sb.append("}");
		return this.forwardAjax(sb.toString(), response);
	}
	
	public ActionForward getInPkOutPieChart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		ChartForm objChartForm = (ChartForm)form;
		// 页面传递过来的当前登陆用户实例
		PubUserVO objPubUserVO = this.getLoginUser(request);
		// 准备查询当月数据
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		// 管理员可以查看组所有数据
		if(!objPubUserVO.equals(objPubUserVO.getUserGroup().getGroupAdmin())){
			lstConditionUtils.add(new ConditionUtil("and", "FeeVO.pubUser.userId", "=", objPubUserVO.getUserId()+"", false));
		}
		lstConditionUtils.add(new ConditionUtil("between", "FeeVO.feeDate", "", objChartForm.getStartDate()+","+objChartForm.getEndDate(), true));
		lstConditionUtils.add(new ConditionUtil("group", "FeeVO.feeType.feeType", "", "", false));
		// 返回汇总 [消费类型,消费金额]
		List<Object[]> lstDatas = this.feeService.groupFeeVOByFeeType(lstConditionUtils);
		StringBuffer sb = new StringBuffer(128);
		sb.append("{");
		sb.append("\"title\":\""+objChartForm.getStartDate()+","+objChartForm.getEndDate()+"\",");
		sb.append("\"data\":[");
		// 放入FeeBean,方便处理数据
		FeeBean objFeeBean = new FeeBean();
		for(int i=0;i<lstDatas.size();i++) {
			Object[] objs = lstDatas.get(i);
			int iFeeType = Integer.parseInt(objs[0].toString());
			if(Constants.FEE_TYPE_IN == iFeeType){
				objFeeBean.setFeeInAmount(MathUtil.round((Double)objs[1], 2).floatValue());
			}else if(Constants.FEE_TYPE_OUT == iFeeType){
				objFeeBean.setFeeOutAmount(MathUtil.round((Double)objs[1], 2).floatValue());
			}
		}
		sb.append("{");
		sb.append("\"name\":\"支出\",");
		sb.append("\"y\":"+objFeeBean.getFeeOutAmount());
		sb.append("},");
		sb.append("{");
		sb.append("\"name\":\"收入\",");
		sb.append("\"y\":"+objFeeBean.getFeeInAmount());
		sb.append("}");
		sb.append("]");
		sb.append(",\"subtitle\":\"结余:"+MathUtil.round(MathUtil.sub(objFeeBean.getFeeInAmount(), objFeeBean.getFeeOutAmount()), 2)+"\"");
		sb.append("}");
		return this.forwardAjax(sb.toString(), response);
	}
	
	public ActionForward getSubPie(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		ChartForm objChartForm = (ChartForm)form;
		PubUserVO objPubUserVO = this.getLoginUser(request);
		// 准备查询当月数据
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		// 管理员可以查看组所有数据
		if(!objPubUserVO.equals(objPubUserVO.getUserGroup().getGroupAdmin())){
			lstConditionUtils.add(new ConditionUtil("and", "FeeVO.pubUser.userId", "=", objPubUserVO.getUserId()+"", false));
		}
		lstConditionUtils.add(new ConditionUtil("and", "FeeVO.feeSubType.feeTypeId", "=", "FeeTypeVO.feeTypeId", false));
		lstConditionUtils.add(new ConditionUtil("and", "FeeTypeVO.feeType", "=", 0+"", false));
		lstConditionUtils.add(new ConditionUtil("and", "FeeVO.feeType.feeTypeId", "=", objChartForm.getFeeTypeId()+"", false));
		lstConditionUtils.add(new ConditionUtil("between", "FeeVO.feeDate", "", objChartForm.getStartDate()+","+objChartForm.getEndDate(), true));
		lstConditionUtils.add(new ConditionUtil("group", "FeeVO.feeSubType.feeTypeId", "", "", false));
		
		// 返回汇总 [消费类型ID,消费类型名称,消费金额]
		List<Object[]> lstDatas = this.feeService.groupFeeVOBySubFeeTypeId(lstConditionUtils);
		StringBuffer sb = new StringBuffer(128);
		sb.append("{");
		sb.append("\"title\":\""+objChartForm.getStartDate()+","+objChartForm.getEndDate()+"\",");
		sb.append("\"data\":[");
		float fcount = 0f;
		for(int i=0;i<lstDatas.size();i++) {
			Object[] objs = lstDatas.get(i);
			sb.append("{");
			sb.append("\"name\":");
			sb.append("\""+objs[1]+"\"");
			sb.append(",\"y\":");
			// 格式化金额
			sb.append(MathUtil.round((Double)objs[2], 2));
			sb.append(",\"feeTypeId\":");
			sb.append(objs[0]);
			sb.append(",\"chartType\":");
			sb.append("\""+objChartForm.getChartType()+"\"");
			sb.append("}");
			if(i<lstDatas.size()-1){
				sb.append(",");
			}
			// 总金额
			fcount = MathUtil.round(fcount+Float.parseFloat(objs[2].toString()), 2).floatValue();
		}
		sb.append("]");
		sb.append(",\"subtitle\":\""+(objChartForm.getFeeType()==0?"总支出":"总收入")+":"+fcount+"\"");
		sb.append("}");
		return this.forwardAjax(sb.toString(), response);
	}
	
	public ActionForward getLineChart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		ChartForm objChartForm = (ChartForm)form;
		PubUserVO objPubUserVO = this.getLoginUser(request);
		// 准备查询当月数据
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		// 管理员可以查看组所有数据
		if(!objPubUserVO.equals(objPubUserVO.getUserGroup().getGroupAdmin())){
			lstConditionUtils.add(new ConditionUtil("and", "FeeVO.pubUser.userId", "=", objPubUserVO.getUserId()+"", false));
		}
		lstConditionUtils.add(new ConditionUtil("and", "FeeVO.feeType.feeType", "=", "0", false));
		lstConditionUtils.add(new ConditionUtil("between", "FeeVO.feeDate", "", objChartForm.getStartDate()+","+objChartForm.getEndDate(), true));
		// 返回汇总 [消费日期,消费金额]
		List<Object[]> lstDatas = new ArrayList<Object[]>();
		// 年和月的分组关键字不一样
		Date[] dMonthDayDates = null;
		if(objChartForm.getChartType().equals(Constants.CHART_TYPE_MONTH)){
			// 管理员可以查看所有
			lstConditionUtils.add(new ConditionUtil("group", "FeeVO.feeDate", "", "", false));
			lstDatas = this.feeService.groupFeeVOByFeeDate(lstConditionUtils);
			// 初始化x轴
			dMonthDayDates = DateUtil.getMonthDayDates(objChartForm.getStartDate().substring(0, objChartForm.getStartDate().lastIndexOf("-")));
		}else if(objChartForm.getChartType().equals(Constants.CHART_TYPE_YEAR)){
			lstConditionUtils.add(new ConditionUtil("group", "DATE_FORMAT(FeeVO.feeDate,'%Y-%m')", "", "", false));
			lstDatas = this.feeService.groupLastYearMonthByFeeDate(lstConditionUtils);
		}
		// 初始化x轴的默认值为0
		Map<String, MonthLineBean> mapMonthLineBeans = new HashMap<String, MonthLineBean>();
		for(Date dMonthDayDate : dMonthDayDates){
			MonthLineBean objMonthLineBean = new MonthLineBean();
			String strDay = DateUtil.Date2String(dMonthDayDate, "yyyy-MM-dd");
			objMonthLineBean.setDay(strDay);
			mapMonthLineBeans.put(strDay, objMonthLineBean);
		}
		// 数据库数据覆盖默认值
		for(Object[] objDatas : lstDatas){
			String strDay = DateUtil.Date2String((Date)objDatas[0], "yyyy-MM-dd");
			float fAmount = MathUtil.round((Double)objDatas[1], 2).floatValue();
			mapMonthLineBeans.get(strDay).setAmount(fAmount);
		}
		StringBuffer sb = new StringBuffer(128);
		StringBuffer sb2 = new StringBuffer(128);
		sb.append("{");
		sb.append("\"title\":\""+"消费走势图\",");
		sb.append("\"series\":[{");
		sb.append("\"name\":\""+objPubUserVO.getName()+"\",");
		//sb.append("\"color\":\"#C3605D\",");
		sb.append("\"data\":[");
		sb2.append("\"categories\":[");
		for(int i=0;i<dMonthDayDates.length;i++){
			String strDay = DateUtil.Date2String(dMonthDayDates[i], "yyyy-MM-dd");
			MonthLineBean objMonthLineBean = mapMonthLineBeans.get(strDay);
			sb.append("{");
			sb.append("\"y\":\"");
			sb.append(objMonthLineBean.getAmount()+"\"");
			sb.append(",\"chartType\":\""+objChartForm.getChartType()+"\"");
			// 格式化日期
			if(objChartForm.getChartType().equals(Constants.CHART_TYPE_MONTH)){
				sb2.append("\""+objMonthLineBean.getDay()+"\"");
			}else if(objChartForm.getChartType().equals(Constants.CHART_TYPE_YEAR)){
				sb2.append("\""+objMonthLineBean.getDay()+"\"");
			}
			sb.append("}");
			if(i!=dMonthDayDates.length-1){
				sb.append(",");
				sb2.append(",");
			}
		}
		sb.append("]}");
		sb.append("],");
		sb2.append("]");
		// categories加入到json
		sb.append(sb2);
		sb.append("}");
		
		return this.forwardAjax(sb.toString(), response);
	}
	
	public ActionForward getComboChart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		ChartForm chartForm = (ChartForm)form;
		// 页面传递过来的当前登陆用户实例
		PubUserVO objPubUserVO = this.getLoginUser(request);
		// 准备查询当月数据
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		// 管理员可以查看组所有数据
		if(!objPubUserVO.equals(objPubUserVO.getUserGroup().getGroupAdmin())){
			lstConditionUtils.add(new ConditionUtil("and", "FeeVO.pubUser.userId", "=", objPubUserVO.getUserId()+"", false));
		}
		// 时间条件
		lstConditionUtils.add(new ConditionUtil("between", "FeeVO.feeDate", "", chartForm.getStartDate()+","+chartForm.getEndDate(), true));
		// Group by 费用类型:支出,收入
		lstConditionUtils.add(new ConditionUtil("group", "DATE_FORMAT(FeeVO.feeDate,'%Y-%m'),FeeVO.feeType.feeType", "", "", false));
		// 查询数据
		List<Object[]> lstDatas = this.feeService.groupLastYearMonthByFeeType(lstConditionUtils);
		StringBuffer sbMain = new StringBuffer(128);
		// serial
		// 收入柱形图
		StringBuffer sbSeriesIn = new StringBuffer(64);
		// 支出柱形图
		StringBuffer sbSeriesOut = new StringBuffer(64);
		// 结余柱形图:收入-支出
		StringBuffer sbSeriesBalance = new StringBuffer(64);
		// 收入线形图
		StringBuffer sbSeriesInSpline = new StringBuffer(64);
		// 支出线形图
		StringBuffer sbSeriesOutSpline = new StringBuffer(64);
		// 结余线形图:收入-支出
		StringBuffer sbSeriesBalanceSpline = new StringBuffer(64);
		// 分类category
		StringBuffer sbCate = new StringBuffer(62);
		// 数据结构保存到自定义的bean中.方便处理数据
		Map<String, FeeBean> map = new HashMap<String, FeeBean>();
		List<String> lstDates = new ArrayList<String>();
		for(int i=0;i<lstDatas.size();i++){
			Object[] objProps = lstDatas.get(i);
			// 日期
			String strDate = (String)objProps[0];
			Integer iFeeType = (Integer)objProps[1];
			Float fAmount = MathUtil.round((Double)objProps[2], 2).floatValue();
			// 日期容器
			boolean isHave = false;
			for(String strDate2 : lstDates){
				if(strDate2.equals(strDate)){
					isHave = true;
				}
			}
			if(!isHave){
				lstDates.add(strDate);
			}
			// 如果数据日期不在Map中则添加
			if(map.get(strDate) == null){
				map.put(strDate, new FeeBean());
			}
			// 取出对应的FeeBean,放入数据
			if(iFeeType == Constants.FEE_TYPE_OUT){// 支出
				map.get(strDate).setFeeOutAmount(fAmount);
			}else if(iFeeType == Constants.FEE_TYPE_IN){// 收入
				map.get(strDate).setFeeInAmount(fAmount);
			}else{
				this.log.error("费用类型不存在");
			}
		}
		// categories
		sbCate.append("\"categories\":[");
		// 收入柱形图
		sbSeriesIn.append("{");
		sbSeriesIn.append("\"type\":\"column\",");
		sbSeriesIn.append("\"name\":\"收入\",");
		sbSeriesIn.append("\"color\":\"#5F8BC0\",");
		sbSeriesIn.append("\"data\":[");
		// 支出柱形图
		sbSeriesOut.append("{");
		sbSeriesOut.append("\"type\":\"column\",");
		sbSeriesOut.append("\"name\":\"支出\",");
		sbSeriesOut.append("\"color\":\"#C3605D\",");
		sbSeriesOut.append("\"data\":[");
		// 结余柱形图
		sbSeriesBalance.append("{");
		sbSeriesBalance.append("\"type\":\"column\",");
		sbSeriesBalance.append("\"name\":\"结余\",");
		sbSeriesBalance.append("\"color\":\"#A2BE68\",");
		sbSeriesBalance.append("\"data\":[");
		// 收入线形图
		sbSeriesInSpline.append("{");
		sbSeriesInSpline.append("\"type\":\"spline\",");
		sbSeriesInSpline.append("\"name\":\"收入\",");
		sbSeriesInSpline.append("\"color\":\"#5F8BC0\",");
		sbSeriesInSpline.append("\"data\":[");
		// 支出线形图
		sbSeriesOutSpline.append("{");
		sbSeriesOutSpline.append("\"type\":\"spline\",");
		sbSeriesOutSpline.append("\"name\":\"支出\",");
		sbSeriesOutSpline.append("\"color\":\"#C3605D\",");
		sbSeriesOutSpline.append("\"data\":[");
		// 结余线形图
		sbSeriesBalanceSpline.append("{");
		sbSeriesBalanceSpline.append("\"type\":\"spline\",");
		sbSeriesBalanceSpline.append("\"name\":\"结余\",");
		sbSeriesBalanceSpline.append("\"color\":\"#A2BE68\",");
		sbSeriesBalanceSpline.append("\"data\":[");
		for(int i=0;i<lstDates.size();i++){
			String strDate = lstDates.get(i);
			sbCate.append("\""+strDate+"\"");
			// series
			FeeBean feeBean = map.get(strDate);
			// 柱形图
			sbSeriesIn.append(feeBean.getFeeInAmount());
			sbSeriesOut.append(feeBean.getFeeOutAmount());
			sbSeriesBalance.append(MathUtil.round(feeBean.getFeeInAmount()-feeBean.getFeeOutAmount(), 2));
			// 线形图
			sbSeriesInSpline.append(feeBean.getFeeInAmount());
			sbSeriesOutSpline.append(feeBean.getFeeOutAmount());
			sbSeriesBalanceSpline.append(MathUtil.round(feeBean.getFeeInAmount()-feeBean.getFeeOutAmount(), 2));
			if(i<lstDates.size()-1){
				sbCate.append(",");
				// 柱形图
				sbSeriesIn.append(",");
				sbSeriesOut.append(",");
				sbSeriesBalance.append(",");
				// 线形图
				sbSeriesInSpline.append(",");
				sbSeriesOutSpline.append(",");
				sbSeriesBalanceSpline.append(",");
			}
		}
		sbCate.append("]");
		// 柱形图
		sbSeriesIn.append("]}");
		sbSeriesOut.append("]}");
		sbSeriesBalance.append("]}");
		// 线形图
		sbSeriesInSpline.append("]}");
		sbSeriesOutSpline.append("]}");
		sbSeriesBalanceSpline.append("]}");
		// 饼图
		Float fFeeOutCount = 0f;
		Float fFeeInCount = 0f;
		// 统计支出和消费总额
		for(Iterator<FeeBean> iterFeeBeans=map.values().iterator();iterFeeBeans.hasNext();){
			FeeBean feeBean = iterFeeBeans.next();
			fFeeOutCount += feeBean.getFeeOutAmount();
			fFeeInCount += feeBean.getFeeInAmount();
		}
		StringBuffer sbPie = new StringBuffer(128);
		sbPie.append("{");
		sbPie.append("\"type\": \"pie\",");
		sbPie.append("\"name\": \"BalanceAndOut\",");
		sbPie.append("\"data\": [{");
		sbPie.append("\"name\": \"支出\",");
		sbPie.append("\"color\": \"#C3605D\",");
		sbPie.append("\"y\": "+MathUtil.round(fFeeOutCount, 2));
		sbPie.append("}, {");
		sbPie.append("\"name\": \"结余\",");
		sbPie.append("\"color\": \"#A2BE68\",");
		sbPie.append("\"y\": "+MathUtil.round(fFeeInCount-fFeeOutCount, 2));
		sbPie.append("}],");
		sbPie.append("\"center\": [30, 30],");
		sbPie.append("\"size\": 50,");
		sbPie.append("\"showInLegend\": false");
		// 显示指引线
		sbPie.append(",\"dataLabels\": {");
		sbPie.append("\"enabled\": false");
		sbPie.append("}");
		sbPie.append("}");
		// 拼装JSON
		sbMain.append("{");
		sbMain.append(sbCate).append(",");
		sbMain.append("\"series\":[");
		sbMain.append(sbSeriesIn).append(",");
		sbMain.append(sbSeriesOut).append(",");
		sbMain.append(sbSeriesBalance).append(",");
		sbMain.append(sbSeriesInSpline).append(",");
		sbMain.append(sbSeriesOutSpline).append(",");
		sbMain.append(sbSeriesBalanceSpline).append(",");
		sbMain.append(sbPie);
		sbMain.append("]");
		sbMain.append("}");
		return this.forwardAjax(sbMain.toString(), response);
	}
	
	public ActionForward initGetContributeChart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("getContributeChart");
	}
	
	public ActionForward getContributeChart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		ChartForm chartForm = (ChartForm)form;
		// 页面传递过来的当前登陆用户
		PubUserVO objPubUserVO = (PubUserVO)this.getLoginUser(request);
		// 准备查询当月数据
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		// 管理员可以查看组所有数据
		if(!objPubUserVO.equals(objPubUserVO.getUserGroup().getGroupAdmin())){
			lstConditionUtils.add(new ConditionUtil("and", "TransferAccountVO.pubUser.userId", "=", objPubUserVO.getUserId()+"", false));
		}
		// 时间条件
		Date[] dYearMonths = DateUtil.getYearMonthDates(chartForm.getYearDate(), "yyyy");
		String startDate = DateUtil.Date2String(DateUtil.getMonthFirstDay(DateUtil.Date2String(dYearMonths[0], "yyyy-MM-dd"), "yyyy-MM-dd"), "yyyy-MM-dd");
		String endDate = DateUtil.Date2String(DateUtil.getMonthLastDay(DateUtil.Date2String(dYearMonths[dYearMonths.length-1], "yyyy-MM-dd"), "yyyy-MM-dd"), "yyyy-MM-dd");
		lstConditionUtils.add(new ConditionUtil("between", "TransferAccountVO.transferDate", "", startDate+","+endDate, true));
		// 转入账户是大管家
		lstConditionUtils.add(new ConditionUtil("and", "TransferAccountVO.toAccount.pubUser.userId", "=", objPubUserVO.getUserGroup().getGroupAdmin().getUserId()+"", false));
		// Group by 费用类型:支出,收入
		lstConditionUtils.add(new ConditionUtil("group", "DATE_FORMAT(TransferAccountVO.transferDate,'%Y-%m'),TransferAccountVO.pubUser.name", "", "", false));
		// 查询汇入大管家账户的数据
		List<Object[]> lstInDatas = this.transferAccountService.groupYearGaveContributeByUser(lstConditionUtils);
		// 查询从大管家账户汇出的数据
		lstConditionUtils.clear();
		lstConditionUtils.add(new ConditionUtil("and", "TransferAccountVO.fromAccount.pubUser.userId", "=", objPubUserVO.getUserGroup().getGroupAdmin().getUserId()+"", false));
		lstConditionUtils.add(new ConditionUtil("between", "TransferAccountVO.transferDate", "", startDate+","+endDate, true));
		lstConditionUtils.add(new ConditionUtil("group", "DATE_FORMAT(TransferAccountVO.transferDate,'%Y-%m'),TransferAccountVO.toAccount.pubUser.name", "", "", false));
		List<Object[]> lstOutDatas = this.transferAccountService.groupYearLostContributeByUser(lstConditionUtils);
		// 数据放入到TransferAccountBean中,便于处理字符串拼接
		Map<String, List<TransferAccountBean>> mapTransferAccountBean = new HashMap<String, List<TransferAccountBean>>();
		for(Iterator<PubUserVO> iterUsers = objPubUserVO.getUserGroup().getGroupUsers().iterator();iterUsers.hasNext();){
			PubUserVO objGroupUser = iterUsers.next();
			// 管理员可以查看所有的,不同用户只能查看自己的
			if(!objPubUserVO.equals(objPubUserVO.getUserGroup().getGroupAdmin())){
				if(objPubUserVO.getUserId() != objGroupUser.getUserId()){
					continue ;
				}
			}
			List<TransferAccountBean> lstTransferAccountBeans = new ArrayList<TransferAccountBean>();
			mapTransferAccountBean.put(objGroupUser.getName(), lstTransferAccountBeans);
			// 放入一年中每个月的日期和默认金额
			for(Date dYearMonth : dYearMonths){
				TransferAccountBean objTransferAccountBean = new TransferAccountBean();
				objTransferAccountBean.setMonthDate(DateUtil.Date2String(dYearMonth, "yyyy-MM"));
				lstTransferAccountBeans.add(objTransferAccountBean);
			}
		}
		// 把大管家从map中删除,因为他不需要显示贡献度
		mapTransferAccountBean.remove(objPubUserVO.getUserGroup().getGroupAdmin().getName());
		// 用查询出来的汇入大管家数据覆盖map中的数据
		for(Object[] objDatas : lstInDatas){
			String strDate = (String)objDatas[0];
			String strName = (String)objDatas[1];
			Double dAmount = (Double)objDatas[2];
			List<TransferAccountBean> lstTransferAccountBeans = mapTransferAccountBean.get(strName);
			for(TransferAccountBean objTransferAccountBean : lstTransferAccountBeans){
				if(objTransferAccountBean.getMonthDate().equals(strDate)){
					objTransferAccountBean.setAmount(MathUtil.round(dAmount, 2).floatValue());
				}
			}
		}
		// 用查询出来的汇出大管家数据覆盖map中的数据
		for(Object[] objDatas : lstOutDatas){
			String strDate = (String)objDatas[0];
			String strName = (String)objDatas[1];
			Double dAmount = (Double)objDatas[2];
			List<TransferAccountBean> lstTransferAccountBeans = mapTransferAccountBean.get(strName);
			if(lstTransferAccountBeans == null){ // 汇出集合中包含普通用户之外其他用户的信息,这里起到过滤作用
				continue ;
			}
			for(TransferAccountBean objTransferAccountBean : lstTransferAccountBeans){
				if(objTransferAccountBean.getMonthDate().equals(strDate)){
					objTransferAccountBean.setAmount(MathUtil.round(objTransferAccountBean.getAmount()-dAmount, 2).floatValue());
				}
			}
		}
		if(Constants.CHART_TYPE_COLUMN.equals(chartForm.getChartType())){ /** 堆图的JSON */
			StringBuffer sb = new StringBuffer(512);
			sb.append("{");
			sb.append("\"series\":[");
			// 根据map中的实例生成json字符串
			for(Iterator<String> iterNames=mapTransferAccountBean.keySet().iterator();iterNames.hasNext();){
				String strName = iterNames.next();
				List<TransferAccountBean> lstTransferAccountBeans = mapTransferAccountBean.get(strName);
				sb.append("{");
				sb.append("\"name\":"+"\""+strName+"\",");
				sb.append("\"data\":[");
				for(int i=0;i<lstTransferAccountBeans.size();i++){
					TransferAccountBean objTransferAccountBean = lstTransferAccountBeans.get(i);
					sb.append(objTransferAccountBean.getAmount());
					if(i<lstTransferAccountBeans.size()-1){
						sb.append(",");
					}
				}
				sb.append("]");
				sb.append("}");
				sb.append(",");
			}
			// 截取掉最后一个逗号
			StringBuffer sbJson = new StringBuffer(512);
			sbJson.append(sb.substring(0, sb.length()-1));
			sbJson.append("]");
			// 拼接日期部分
			sbJson.append(",\"categories\":[");
			for(int i=0;i<dYearMonths.length;i++){
				sbJson.append("\""+DateUtil.Date2String(dYearMonths[i], "yyyy-MM")+"\"");
				if(i<dYearMonths.length-1){
					sbJson.append(",");
				}
			}
			sbJson.append("],\"title\":\"贡献值统计\"");
			sbJson.append(",\"subtitle\":\""+chartForm.getYearDate()+"年度\"");
			sbJson.append("}");
			return this.forwardAjax(sbJson.toString(), response);
		}else if(Constants.CHART_TYPE_PIE.equals(chartForm.getChartType())){ /** 饼图的JSON */
			StringBuffer sb = new StringBuffer(128);
			sb.append("{");
			sb.append("\"data\":[");
			for(Iterator<String> iterNames=mapTransferAccountBean.keySet().iterator();iterNames.hasNext();){
				String strName = iterNames.next();
				List<TransferAccountBean> lstTransferAccountBeans = mapTransferAccountBean.get(strName);
				float fAmount = 0f;
				for(TransferAccountBean objTransferAccountBean : lstTransferAccountBeans){
					fAmount += objTransferAccountBean.getAmount();
				}
				sb.append("{");
				sb.append("\"name\":\""+strName+"\",");
				sb.append("\"y\":"+MathUtil.round(fAmount, 2));
				sb.append("},");
			}
			StringBuffer sbJson = new StringBuffer(128);
			sbJson.append(sb.substring(0, sb.length()-1));
			sbJson.append("],");
			sbJson.append("\"title\":\"贡献度统计\",");
			sbJson.append("\"subtitle\":\""+chartForm.getYearDate()+"年度\"");
			sbJson.append("}");
			return this.forwardAjax(sbJson.toString(), response);
		}else{
			this.log.error("没有对应的报表类型:"+chartForm.getChartType());
			return null;
		}
	}
	
	public ActionForward initGetOutInBalanceChart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("getOutInBalanceChart");
	}
	
}
